<?php
	/*
		PandaForum: PHP Forum Engine
		
		Jesse Gill, 0108377707
	*/
	
	define("MYSQL_TRUE", 1);
	define("MYSQL_FALSE", 0);
	
	class DBConnection
	{
		public $connection = false;
		private $error = "";
		private $error_code = 0;
		private $warning = "";
		
		function __construct()
		{
			
		}
		
		function __destruct()
		{
			$this->close();
		}
		
		function connect($host, $username, $password, $dbname)
		{
			if ($this->is_connected())
			{
				$this->close();
			}
			
			$this->connection = new mysqli($host, $username, $password, $dbname);
			
			if (mysqli_connect_error())
			{
				// connect error
				$this->connection = false;
				$this->error = mysqli_connect_error();
				$this->error_code = mysqli_connect_errno();
				
				return false;
			}
			
			return true;
		}
		
		function is_connected()
		{
			if (! $this->connection)
			{
				return false;
			}
			
			return true;
		}
		
		function get_error()
		{
			return $this->error;
		}
		
		function get_error_code()
		{
			return $this->error_code;
		}
		
		function query($query)
		{
			if (! $this->is_connected())
			{
				return false;
			}
			
			$result = $this->connection->query($query);
			
			if (! $result)
			{
				$this->error = $this->connection->error;
				$this->error_code = $this->connection->errno;
			}
			
			return $result;
		}
		
		function query_noresult($query)
		{
			if (! $this->query($query))
			{
				return 0;
			}
			
			return $this->connection->affected_rows;
		}
		
		function get_result($query)
		{
			$result = $this->query($query);
			
			if (! $result)
			{
				return false;
			}
			
			return $result;
		}
		
		function get_result_array($query)
		{
			$result = $this->get_result($query);
			
			if ($result)
			{
				$arr = $result->fetch_assoc();
				
				$result->free();
				
				return $arr;
			}
			
			return false;
		}
		
		function get_result_object($query)
		{
			$result = $this->get_result($query);
			
			if ($result)
			{
				$obj = $result->fetch_object();
				
				$result->free();
				
				return $obj;
			}
			
			return false;
		}
		
		function clean_var($var)
		{
			if (! $this->is_connected())
			{
				return $var;
			}
			
			if (get_magic_quotes_gpc())
			{
				$var = stripslashes($var);
			}
			
			$var = $this->connection->real_escape_string($var);
			
			return $var;
		}
		
		function close()
		{
			// close the db connection if it is open
			if ($this->is_connected())
			{
				$this->connection->close();
				$this->connection = false;
			}
		}
	}
?>